/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var containsNearbyDuplicate = function (nums, k) {
  if (nums.length <= 0) {
    return false
  }
  let keyMap = {}
  for (let i = 0; i < nums.length; i++) {
    if (!keyMap[nums[i]]) {
      keyMap[nums[i]] = []
    }
    keyMap[nums[i]].push(i)
  }
  for (let i in keyMap) {
    let dom = keyMap[i]
    let status = false // true 代表找到一个不符合条件
    if (dom.length <= 1) {
      continue
    } else {
      for (let j = 1; j < dom.length; j++) {
        // 找到一个符合条件的
        if (dom[j] - (dom[j - 1] || 0) <= k) {
          status = true
          return true
        }
      }
    }
    if (status) {
      return status
    }
  }
  return false
};

let nums = [1, 2, 3, 1, 2, 3]
let k = 2
console.log('结果', containsNearbyDuplicate(nums, 2))